﻿using black_screen_mode_server;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

public class IniFile
{
    private string _path;

    public IniFile(string path)
    {
        _path = path;
    }

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


    [DllImport("kernel32.dll")]
    private static extern long WritePrivateProfileString(string section, byte[] key, byte[] val, string filePath);

    [DllImport("kernel32.dll")]
    private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);

    public string Read(string section, string key)
    {
        //var retVal = new StringBuilder(255);
        //GetPrivateProfileString(section, key, "", retVal, 255, _path);
        try
        {
            byte[] Buffer = new byte[1024];
            int bufLen = GetPrivateProfileString(section, key, "", Buffer, Buffer.GetUpperBound(0), _path);
            string s = Encoding.UTF8.GetString(Buffer, 0, bufLen);
            Logger.Singleton.Info("s:" + s);
            return s;
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }
    public void Write(string section, string key, string value)
    {
        WritePrivateProfileString(section,Encoding.UTF8.GetBytes(key), Encoding.UTF8.GetBytes(value), _path);
    }
}
